<template>
  <el-dialog
    v-model="visible"
    :title="$t('crud.updateBtn')"
    :close-on-click-modal="false"
  >
    <el-form
      ref="dataFormRef"
      :model="dataForm"
      label-width="120px"
      @submit.prevent
      @keyup.enter="onSubmit()"
    >
      <el-form-item
        :label="$t('distribUserWallet.withdrawalAmount')"
      >
        <el-input
          v-model="dataForm.settledAmount"
          type="number"
          min="0"
          oninput="if(value.length>13)value=value.slice(0,13)"
          @change="(value)=>{if(value>9999999999.99)$nextTick(()=>{dataForm.settledAmount = 9999999999.99})}"
        />
      </el-form-item>
    </el-form>
    <template #footer>
      <div class="dialog-footer">
        <div
          class="default-btn"
          @click="visible = false"
        >
          {{ $t("remindPop.cancel") }}
        </div>
        <div
          class="default-btn primary-btn"
          @click="onSubmit()"
        >
          {{ $t("remindPop.confirm") }}
        </div>
      </div>
    </template>
  </el-dialog>
</template>

<script setup>
import { ElMessage } from 'element-plus'
import Big from 'big.js'
import { ref, watch } from 'vue'

const emit = defineEmits(['refreshDataList'])

const visible = ref(false)
const dataForm = ref({
  walletId: null,
  distributionUserId: null,
  unsettledAmount: null,
  settledAmount: null,
  invalidAmount: null,
  addupAmount: null,
  version: null
})
const dataFormRef = ref(null)
let oldSettledAmount = null
watch(dataForm, () => {
  if (dataForm.value.addupAmount <= 0) {
    dataForm.value.addupAmount = 0
  }
  if (dataForm.value.invalidAmount <= 0) {
    dataForm.value.invalidAmount = 0
  }
  if (dataForm.value.settledAmount <= 0) {
    dataForm.value.settledAmount = 0
  }
  if (dataForm.value.unsettledAmount <= 0) {
    dataForm.value.unsettledAmount = 0
  }
}, { deep: true })

const init = (walletData) => {
  dataForm.value = Object.assign({}, walletData)
  oldSettledAmount = dataForm.value.settledAmount
  visible.value = true
}
// 表单提交
const onSubmit = () => {
  dataForm.value.changeAmount = parseFloat(Big(dataForm.value.settledAmount).minus(oldSettledAmount))
  http({
    url: http.adornUrl('/distribution/distributionUserWallet'),
    method: 'put',
    data: http.adornData(dataForm.value)
  }).then(() => {
    ElMessage({
      message: $t('publics.operation'),
      type: 'success',
      duration: 1500,
      onClose: () => {
        visible.value = false
        emit('refreshDataList')
      }
    })
  })
}

defineExpose({
  init
})
</script>
